package com.example.eshop_ssm.util;

import org.apache.log4j.Logger;
import org.springframework.core.annotation.Order;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Order(-10)  //默认是最低优先级,值越小优先级越高
public class GlobalExceptionHandler implements HandlerExceptionResolver {
    private Logger logger = Logger.getLogger(GlobalExceptionHandler.class);
    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        ModelAndView modelAndView = new ModelAndView();

        //e  ->>  异常
        //异常应该让我们开发者自己捕获到，这样就可以记录Log4j日志
        logger.error(null,e);
        //自定义错误页面
        modelAndView.setViewName("error");
        return modelAndView;
    }
}
